Public Class FrmBuscar

#Region "Declaraciones"
    Private _buscar As ClaseBaseMethod
    Public Property Buscar() As ClaseBaseMethod
        Get
            Return _buscar
        End Get
        Set(ByVal Value As ClaseBaseMethod)
            _buscar = Value
        End Set
    End Property
#End Region

    Public Overloads Sub ShowDialog(ByVal BuscarObj As ClaseBaseMethod)
        Buscar = BuscarObj
        Me.ShowDialog()
        Application.DoEvents()
    End Sub

    Private Sub CmbBuscar_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmbBuscar.SelectedIndexChanged
        Select Case Buscar.BuscarObj.ListadoDeTiposDeCampos(CmbBuscar.SelectedIndex)
            Case ClaseEnumObj.TiposDeCampos.Varchar

                TxtBuscar.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.None
                TxtBuscar.Properties.Mask.EditMask = ""
                TxtBuscar.Properties.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Default
                TxtBuscar.Text = ""

            Case ClaseEnumObj.TiposDeCampos.Numerico

                TxtBuscar.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric
                TxtBuscar.Properties.Mask.EditMask = "n0"
                TxtBuscar.Properties.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far
                TxtBuscar.Text = "0"

            Case ClaseEnumObj.TiposDeCampos.Fecha

                TxtBuscar.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.DateTime
                TxtBuscar.Properties.Mask.EditMask = "d"
                TxtBuscar.Properties.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Default
                TxtBuscar.Text = Now.ToShortDateString

            Case Else

                TxtBuscar.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.None
                TxtBuscar.Properties.Mask.EditMask = ""
                TxtBuscar.Properties.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Default
                TxtBuscar.Text = ""

        End Select

        TxtBuscar.Properties.Mask.UseMaskAsDisplayFormat = True

    End Sub

    Private Sub ArreglarColumnas()
        Select Case GrdBuscarView.Columns.Count
            Case 2
                GrdBuscarView.OptionsView.ColumnAutoWidth = True
                GrdBuscarView.Columns(0).Width = 100
            Case Else
                GrdBuscarView.OptionsView.ColumnAutoWidth = False
                For I = 0 To GrdBuscarView.Columns.Count - 1
                    Select Case I
                        Case 0
                            GrdBuscarView.Columns(0).Width = 100
                        Case 1
                            GrdBuscarView.Columns(1).Width = 200
                        Case Else
                            GrdBuscarView.Columns(I).Width = 200
                    End Select
                Next
        End Select
        
    End Sub

    Private Sub TmrFocoGrilla_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TmrFocoGrilla.Tick

        If GrdBuscarView.Columns.Count > 0 Then
            GrdBuscarControl.ForceInitialize()
            GrdBuscarView.Focus()
            GrdBuscarView.FocusedColumn = GrdBuscarView.Columns(1)
            GrdBuscarView.FocusedRowHandle = DevExpress.XtraGrid.GridControl.AutoFilterRowHandle
        Else
            TxtBuscar.Focus()
        End If

        TmrFocoGrilla.Enabled = False
    End Sub

    Private Sub TxtBuscar_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TxtBuscar.KeyDown
        If e.KeyCode = Keys.Enter Then
            BtnBuscar_Click(Nothing, Nothing)
        End If
    End Sub

    Private Sub FrmBuscar_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.Dispose()
        End If
    End Sub

    Private Sub BtnAceptar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAceptar.Click
        Dim FilaElejida As Integer = GrdBuscarView.FocusedRowHandle
        If FilaElejida <> DevExpress.XtraGrid.GridControl.AutoFilterRowHandle And FilaElejida <> DevExpress.XtraGrid.GridControl.InvalidRowHandle And FilaElejida <> DevExpress.XtraGrid.GridControl.NewItemRowHandle Then
            BuscarObj.ElCodigoBuscado(0) = 1
            BuscarObj.ElCodigoBuscado(1) = GrdBuscarView.GetRowCellDisplayText(GrdBuscarView.GetSelectedRows()(0), GrdBuscarView.VisibleColumns(0))
            BuscarObj.ElCodigoBuscado(2) = GrdBuscarView.GetRowCellDisplayText(GrdBuscarView.GetSelectedRows()(0), GrdBuscarView.VisibleColumns(1))
            Me.Dispose()
        Else
            Me.Dispose()
        End If
    End Sub

    Private Sub GrdBuscarControl_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles GrdBuscarControl.KeyDown
        On Error Resume Next
        If e.KeyCode = Keys.Enter Then
            Dim FilaElejida As Integer = GrdBuscarView.FocusedRowHandle
            If FilaElejida <> DevExpress.XtraGrid.GridControl.AutoFilterRowHandle And FilaElejida <> DevExpress.XtraGrid.GridControl.InvalidRowHandle And FilaElejida <> DevExpress.XtraGrid.GridControl.NewItemRowHandle Then
                BuscarObj.ElCodigoBuscado(0) = 1
                BuscarObj.ElCodigoBuscado(1) = GrdBuscarView.GetRowCellDisplayText(GrdBuscarView.GetSelectedRows()(0), GrdBuscarView.VisibleColumns(0))
                BtnAceptar_Click(sender, e)
            End If
        End If
    End Sub

    Private Sub GrdBuscarControl_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GrdBuscarControl.DoubleClick
        On Error Resume Next

        Dim FilaElejida As Integer = GrdBuscarView.FocusedRowHandle
        If FilaElejida <> DevExpress.XtraGrid.GridControl.AutoFilterRowHandle And FilaElejida <> DevExpress.XtraGrid.GridControl.InvalidRowHandle And FilaElejida <> DevExpress.XtraGrid.GridControl.NewItemRowHandle Then
            BuscarObj.ElCodigoBuscado(0) = 1
            BuscarObj.ElCodigoBuscado(1) = GrdBuscarView.GetRowCellDisplayText(GrdBuscarView.GetSelectedRows()(0), GrdBuscarView.VisibleColumns(0))
            BtnAceptar_Click(sender, e)
        End If
    End Sub

    Private Sub FrmBuscar_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        For Each Item In Buscar.BuscarObj.ListadoDeComboCampos
            CmbBuscar.Properties.Items.Add(Item)
        Next

        CmbBuscar.SelectedIndex = Buscar.BuscarObj.ComboIndiceElejido

        ArreglarColumnas()
    End Sub

    Private Sub BtnBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnBuscar.Click

        Try
            If Buscar.BuscarObj.SqlIdExtrangero = "" Then

                GrdBuscarControl.DataSource = Nothing
                GrdBuscarControl.DataMember = Nothing
                GrdBuscarView.Columns.Clear()

                Buscar.BuscarObj.NombreCampoABuscar = Buscar.BuscarObj.ListadoDeNombreCampos(CmbBuscar.SelectedIndex)
                Buscar.BuscarObj.TextoABuscar = TxtBuscar.EditValue
                Buscar.BuscarObj.TipoDeCampoABuscar = Buscar.BuscarObj.ListadoDeTiposDeCampos(CmbBuscar.SelectedIndex)

                GrdBuscarControl.DataSource = Buscar.Buscar(Buscar.BuscarObj)

            Else




            End If

            ArreglarColumnas()

            If GrdBuscarView.RowCount >= 1 Then
                GrdBuscarControl.ForceInitialize()
                GrdBuscarView.Focus()
                GrdBuscarView.FocusedColumn = GrdBuscarView.Columns(1)
                GrdBuscarView.FocusedRowHandle = 0
            End If
        Catch ex As Exception
            DevExpress.XtraEditors.XtraMessageBox.Show(String.Format(My.Resources.ResMensajes.ErrorDesconocido, ex.Message), My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Dispose()
        End Try



    End Sub

    Private Sub BtnImprimir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnImprimir.Click
        try
            Me.GrdBuscarControl.ShowPrintPreview()
        Catch ex As Exception
            DevExpress.XtraEditors.XtraMessageBox.Show(String.Format(My.Resources.ResMensajes.ErrorDesconocido, ex.Message), My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Dispose()
        End Try
    End Sub

    Private Sub BtnExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnExcel.Click
        Try
            Me.GrdBuscarControl.ExportToXls("")
        Catch ex As Exception
            DevExpress.XtraEditors.XtraMessageBox.Show(String.Format(My.Resources.ResMensajes.ErrorDesconocido, ex.Message), My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Dispose()
        End Try
    End Sub
End Class